<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TCP 粘包与拆包问题深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .solution-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            border-left: 4px solid;
            transition: all 0.3s ease;
        }
        
        .solution-card:hover {
            transform: translateX(10px);
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        
        .highlight-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-weight: 700;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    TCP 粘包与拆包问题
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度技术解析</span>
                </h1>
                <p class="text-xl md:text-2xl font-light opacity-90 leading-relaxed">
                    探索 TCP 流特性背后的技术挑战，掌握应用层协议设计的核心要点
                </p>
                <div class="mt-10 flex justify-center space-x-8">
                    <div class="text-center">
                        <i class="fas fa-network-wired text-4xl mb-2"></i>
                        <p class="text-sm opacity-80">网络协议</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-code text-4xl mb-2"></i>
                        <p class="text-sm opacity-80">实战方案</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-4xl mb-2"></i>
                        <p class="text-sm opacity-80">最佳实践</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-12 animate-fadeInUp">
            <div class="prose max-w-none">
                <p class="text-lg leading-relaxed text-gray-700 first-letter:text-6xl first-letter:font-bold first-letter:text-purple-600 first-letter:mr-3 first-letter:float-left">
                    TCP 的粘包和拆包问题源于 TCP 流的特性和网络层的传输机制。由于 TCP 协议本身不处理应用层消息的边界问题，应用程序需要通过设计有效的协议来解决这些问题。常见的解决方案包括固定长度消息、分隔符、长度字段和自定义协议。
                </p>
            </div>
        </div>

        <!-- Concept Visualization -->
        <div class="mb-16 animate-fadeInUp" style="animation-delay: 0.2s">
            <h2 class="text-3xl font-bold text-center mb-8 text-gray-800">
                <i class="fas fa-diagram-project mr-3 text-purple-600"></i>
                概念关系图
            </h2>
            <div class="mermaid">
                graph TD
                    A[TCP协议] -->|面向字节流| B[数据传输]
                    B --> C{传输特性}
                    C -->|数据合并| D[粘包问题]
                    C -->|数据分割| E[拆包问题]
                    D --> F[多个消息合并]
                    E --> G[单个消息分割]
                    F --> H[应用层解决方案]
                    G --> H
                    H --> I[固定长度]
                    H --> J[分隔符]
                    H --> K[长度字段]
                    H --> L[自定义协议]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style H fill:#764ba2,stroke:#fff,stroke-width:3px,color:#fff
                    style D fill:#ff6b6b,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#ff6b6b,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- What is Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-16 animate-fadeInUp" style="animation-delay: 0.3s">
            <div class="content-card rounded-2xl p-8">
                <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-puzzle-piece mr-3 text-purple-600"></i>
                    什么是粘包？
                </h3>
                <div class="space-y-4">
                    <div class="bg-purple-50 rounded-lg p-6 border-l-4 border-purple-500">
                        <p class="text-gray-700 leading-relaxed">
                            <strong class="text-purple-700">粘包</strong>是指接收方将多个发送方的数据包粘在一起，接收到了一个长的数据块，实际上这个长数据块中包含了多个独立的消息。
                        </p>
                    </div>
                    <div class="flex items-center space-x-4 text-gray-600">
                        <i class="fas fa-compress-alt text-3xl text-purple-500"></i>
                        <span>多个消息在网络中被合并成了一个数据包进行接收</span>
                    </div>
                </div>
            </div>
            
            <div class="content-card rounded-2xl p-8">
                <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-cut mr-3 text-purple-600"></i>
                    什么是拆包？
                </h3>
                <div class="space-y-4">
                    <div class="bg-blue-50 rounded-lg p-6 border-l-4 border-blue-500">
                        <p class="text-gray-700 leading-relaxed">
                            <strong class="text-blue-700">拆包</strong>是指发送方发送的数据包被分割成多个 TCP 包到达接收方。接收方接收到了消息的几个部分，数据被拆分成了多个小块。
                        </p>
                    </div>
                    <div class="flex items-center space-x-4 text-gray-600">
                        <i class="fas fa-expand-alt text-3xl text-blue-500"></i>
                        <span>一个完整消息被网络传输特性分割成多个部分</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Why Section -->
        <div class="content-card rounded-2xl p-10 mb-16 animate-fadeInUp" style="animation-delay: 0.4s">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-question-circle mr-3 text-purple-600"></i>
                为什么会发生粘包和拆包？
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-xl p-6 border border-purple-200">
                    <div class="text-purple-600 text-4xl mb-4">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-3 text-gray-800">网络层的拆分</h4>
                    <p class="text-gray-600 leading-relaxed">
                        TCP 协议并没有对应用层的消息边界进行控制，它只负责将数据从源到目的地传输。网络层的传输和协议栈的处理会导致消息被分割或合并。
                    </p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl p-6 border border-blue-200">
                    <div class="text-blue-600 text-4xl mb-4">
                        <i class="fas fa-paper-plane"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-3 text-gray-800">数据的发送方式</h4>
                    <p class="text-gray-600 leading-relaxed">
                        应用程序发送数据时，不同的数据量和发送频率会影响 TCP 包的大小。小数据包可能会被合并，大数据包可能会被分割。
                    </p>
                </div>
                
                <div class="bg-gradient-to-br from-green-50 to-emerald-50 rounded-xl p-6 border border-green-200">
                    <div class="text-green-600 text-4xl mb-4">
                        <i class="fas fa-stream"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-3 text-gray-800">TCP 的流特性</h4>
                    <p class="text-gray-600 leading-relaxed">
                        TCP 是面向字节流的，没有消息的边界。接收方接收到的数据流中可能包含了多个应用层消息，导致粘包和拆包问题。
                    </p>
                </div>
            